package entity;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;

@Entity
@Table(name = "ROLES")	
@NamedQueries({ 
@NamedQuery(name = "Role.selectByName", query = "SELECT r FROM Role r WHERE r.name = :name"), 
@NamedQuery(name = "Role.selectNames", query = "SELECT r.name FROM Role r")
})
public class Role {

	@Id
	@GeneratedValue(strategy=GenerationType.SEQUENCE, generator="DEF_GEN")    
	@SequenceGenerator(name = "DEF_GEN", sequenceName = "ROLE_ID_SEQ", initialValue = 4, allocationSize=1)
	@Column(name = "ROLE_ID")
	private int id;

	@Column(name = "NAME", nullable = false, unique = true)
	private String name;

	public Role() {
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (!(obj instanceof Role))
			return false;
		Role other = (Role) obj;
		if(!(this.getName().equals(other.getName())))
			return false;
		return true;
	}
	
	@Override
	public int hashCode() {		
		int result = 42;
		result = 31 * result + id;
		result = 31 * result + name.hashCode();
		return result;
	}

	@Override
	public String toString() {
		return name;
	}
	
	public int getId() {
		return id;
	}

	public Role(String name) {
		this.name = name;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

}